package com.mm.ai.config;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PreDestroy;
import java.io.IOException;

/**
 * @Author： tzy
 * @Date： 2024/11/18 19:51
 */
@Configuration
public class DockerConfig {
    public static final String DOCKER_HOST="tcp://47.115.8.214:2375";

    private DockerClient dockerClient;

    @Bean
    public DockerClient dockerClient() {
        // 创建 Docker 客户端配置
        DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
                .withDockerHost(DOCKER_HOST)
                .build();

        // 使用配置构建 Docker 客户端
        return DockerClientBuilder.getInstance(config).build();
    }

    public DockerClient getDockerClient() {
        return dockerClient;
    }

    @PreDestroy
    public void destroy() {
        if (dockerClient != null) {
            try {
                dockerClient.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
